<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
</head>
<script src="webjars/jquery/3.2.0/jquery.js"></script>
<body>
<button id="logout">退出登录</button>
<!--表格用户-->
<table border="1" style="width: 1000px;margin: 0 auto">
    <tr>
        <th>编号</th>
        <th>用户</th>
        <th>密码</th>
        <th>类型</th>
        <th>备注</th>
        <th style="margin-left: 50px">操作</th>
    </tr>

    <tr th:each="user : ${users}" style="text-align: center">
        <td th:text="${user.uCode}"></td>
        <td th:text="${user.uName}"></td>
        <td th:text="${user.uPwd}"></td>
        <td th:text="${user.uType == 2 ? '管理员' : '普通用户'}"></td>
        <td th:text="${user.uMemo}"></td>
        <td style="width: 50px">
            <button id="insert" th:onclick="insert('{user.uType})]')">新增</button>
        </td>
        <td style="width: 50px">
            <button id="update" th:onclick="update('[(${user.uCode})]')">修改</button>
        </td>
        <td style="width: 50px">
            <button id="delete" th:onclick="deletes('[(${user.uCode})]')">删除</button>
        </td>
        <td style="width: 50px">
            <button id="select" th:onclick="select('[(${user.uCode})]')">详情</button>
        </td>
    </tr>
</table>
</body>
<script th:inline="javascript">

    $(function () {
        var uId = sessionStorage.getItem("uId");
        if (uId == null) {
            // 请先登录弹框
            var userConfirmed = confirm("请先登录？");
            if (userConfirmed) {
                window.location.href = "http://localhost:8080";
            } else {
                window.location.href = "http://localhost:8080";
            }
        }

        // 退出登录
        $("#logout").click(function () {
            sessionStorage.clear();
            window.location.href = "http://localhost:8080";
        })
    })


    // 删除
    function deletes(uType) {
        var uTypes = sessionStorage.getItem("uType");
        if (uTypes == "2" && sessionStorage.getItem("uId") != uType) {
            // 提示是否要确认删除
            var result = confirm("是否确认删除？");
            if (result) {
                $.ajax({
                    url: "http://localhost:8080/delete/" + uType,
                    type: "GET",
                    success: function (data) {
                        console.log(data);
                        window.location.href = "http://localhost:8080/selectAll";
                    }
                });
            } else {
                return false;
            }
        } else {
            alert("没有权限")
        }
    }

    function insert(uType) {
        var uTypes = sessionStorage.getItem("uType");
        if (uTypes == "2") {
            window.location.href = "http://localhost:8080/inserts";
        } else {
            alert("没有权限")
        }
    }


    // select 查询
    function select(uCode) {
        window.location.href = '/selectById?id=' + uCode;
    }

    // update 修改
    function update(uCode) {
        var uTypes = sessionStorage.getItem("uType");
        var uId = sessionStorage.getItem("uId");
        if (uTypes == "2") {
            window.location.href = '/selectById?id=' + uCode + "&type=update"
        } else {
            if (uId == uCode) {
                window.location.href = '/selectById?id=' + uCode + "&type=update"
            } else {
                alert("没有权限")
            }
        }
    }
</script>
</html>